﻿


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSQuaTrinhCongTac : BusinessBase
    {
        NSQuyetDinh quyetDinh = new NSQuyetDinh();
        public string MaSo { get; set; }
        CommonCode common = new CommonCode();
        NSToChuc toChuc = new NSToChuc();
        DataClassesDataContext db = new DataClassesDataContext();
        public string ThoiGian { get; set; }
        public string TenCoQuan { get; set; }
        public void EditQTCT(int hdId, DateTime ngayBatDau, DateTime ngayKetThuc, string noiDung, int coQuanSuDung, bool flag, string coQuanKhac, string chucVu, string congViec, int qdID)
        {
            List<NSQuaTrinhCongTac> lst = (List<NSQuaTrinhCongTac>)HttpContext.Current.Session["QTCT"];
            NSQuaTrinhCongTac pc = lst.Where(a => a.QuaTrinhCongTacId.Equals(hdId)).SingleOrDefault();
            pc.NgayBatDau = ngayBatDau;
            if (!ngayKetThuc.Year.Equals(1))
            {
                pc.NgayKetThuc = ngayKetThuc;
            }
            pc.ChucVu = chucVu;
            pc.CongViec = congViec;
            pc.NoiDung = noiDung;
            if (!ngayKetThuc.Year.Equals(1))
            {
                pc.ThoiGian = string.Format("{0}-{1}", ngayBatDau.ToString("dd/MM/yyyy"), ngayKetThuc.ToString("dd/MM/yyyy"));
            }
            else
            {
                pc.ThoiGian = ngayBatDau.ToString("dd/MM/yyyy");
            }
           
            pc.CoQuanSuDungID = coQuanSuDung;
            //chỉ thêm mới khi quyết định > 0.
            //Đã tồn tại qdID khi =0
            if (qdID > 0)
            {
                pc.QuyetDinhID = qdID;
                pc.MaSo = quyetDinh.GetMaSo(qdID);
            }
            else
            {
                pc.MaSo = quyetDinh.GetMaSo(pc.QuyetDinhID.Value);
            }
            pc.ToChucFlag = flag;
            if (!flag)
            {
                pc.CoQuanKhac = coQuanKhac;
            }
        }
        public void DeleteQTCT(int hdId)
        {
            List<NSQuaTrinhCongTac> lst = (List<NSQuaTrinhCongTac>)HttpContext.Current.Session["QTCT"];
            NSQuaTrinhCongTac pc = lst.Where(a => a.QuaTrinhCongTacId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllQTCTByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSQuaTrinhCongTacs
                         where a.NhanVienId.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSQuaTrinhCongTacs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateQTCT_NhanVien(int nhanVienId, List<NSQuaTrinhCongTac> obj)
        {

            var queryNBLByNhanVienId = (from a in db.NSQuaTrinhCongTacs
                                        where a.NhanVienId.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSQuaTrinhCongTacs.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSQuaTrinhCongTac pc in obj)
            {
                NSQuaTrinhCongTac item = new NSQuaTrinhCongTac();
                CopyNSQuaTrinhCongTac(pc, item);
                db.NSQuaTrinhCongTacs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSQuaTrinhCongTac(NSQuaTrinhCongTac src, NSQuaTrinhCongTac des)
        {
            des.NgayBatDau = src.NgayBatDau;
            if (src.NgayKetThuc.HasValue)
            {
                des.NgayKetThuc = src.NgayKetThuc;
            }
            des.NoiDung = src.NoiDung;
            des.CoQuanSuDungID = src.CoQuanSuDungID;
            des.ToChucFlag = src.ToChucFlag;
            des.CoQuanKhac = src.CoQuanKhac;
            des.NhanVienId = src.NhanVienId;
            des.CongViec = src.CongViec;
            des.QuyetDinhID = src.QuyetDinhID;
            des.ChucVu = src.ChucVu;
        }
        public List<NSQuaTrinhCongTac> LoadQTCTByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSQuaTrinhCongTacs
                         where a.NhanVienId.Equals(nhanVienId)
                         orderby a.QuaTrinhCongTacId descending
                         select a).ToList();
            foreach (NSQuaTrinhCongTac item in query)
            {
                string ngayKetThuc = string.Empty;
                if (item.NgayKetThuc != null)
                {
                    ngayKetThuc = !item.NgayKetThuc.Value.Year.Equals(0001) ? " - " + item.NgayKetThuc.Value.ToString("dd/MM/yyyy") : string.Empty;
                }
                item.ThoiGian = string.Format("{0}{1}", item.NgayBatDau.Value.ToString("dd/MM/yyyy"), ngayKetThuc);
                if (item.ToChucFlag.HasValue && item.ToChucFlag.Value)
                {
                    item.TenCoQuan = item.CoQuanSuDungID.HasValue && item.CoQuanSuDungID.Value > 0 ? toChuc.LayTenToChuc(item.CoQuanSuDungID.Value) : string.Empty;
                }
                else
                {
                    item.TenCoQuan = item.CoQuanKhac;
                }
                item.MaSo = item.QuyetDinhID.HasValue ? quyetDinh.GetMaSo(item.QuyetDinhID.Value) : string.Empty;

            }
            return query;

        }
        public NSQuaTrinhCongTac GetQTCT(int nhanVienId, DateTime ngayBatDau, DateTime ngayKetThuc, string noiDung, int coquan, bool flag, string coQuanKhac, string chucVu, string congViec, int qdID)
        {
            var db = new DataClassesDataContext();
            NSQuaTrinhCongTac hd = new NSQuaTrinhCongTac();
            hd.QuaTrinhCongTacId = 0 - DateTime.Now.Second;
            hd.NgayBatDau = ngayBatDau;
            if (!ngayKetThuc.Year.Equals(0001))
            {
                hd.NgayKetThuc = ngayKetThuc;
            }
            hd.NoiDung = noiDung;
            hd.ChucVu = chucVu;
            hd.CongViec = congViec;
            hd.CoQuanSuDungID = coquan;
            hd.NhanVienId = nhanVienId;

            hd.ToChucFlag = flag;
            if (!flag)
            {
                hd.CoQuanKhac = coQuanKhac;
            }
            hd.QuyetDinhID = qdID;
            hd.ThoiGian = string.Format("{0}{1}", ngayBatDau.ToString("dd/MM/yyyy"), !ngayKetThuc.Year.Equals(0001) ? " - " + ngayKetThuc.ToString("dd/MM/yyyy") : string.Empty);
            hd.TenCoQuan = flag && coquan > 0 ? toChuc.LayTenToChuc(coquan) : coQuanKhac;
            hd.MaSo = quyetDinh.GetMaSo(qdID);
            return hd;
        }

        public NSQuaTrinhCongTac GetQuaTrinhCongTacMoiNhatByNhanVienID(int nhanVienId)
        {
            var query = (from a in db.NSQuaTrinhCongTacs
                         where a.NhanVienId.Equals(nhanVienId)
                         orderby a.NgayBatDau descending
                         select a).ToList();
            if (query.Count > 0)
            {
                return query[0];
            }
            else return null;
        }
    }
}